package com.OnTimeTeam.OnTime;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


/***************************************************************************
 * @author Shiran Menagen
 * @ClassName: UrlXMLClass
 * @MoreAbout: make the Document out of the xml gotten from the url
 * @Methods: Node getNodeByName(Node fatherNode, String nodeName)
 * 			 Document getDoc()
 **************************************************************************/

public class UrlXMLClass {
	private Document doc;
	//private BufferedReader in;
	
	public UrlXMLClass (String urlString){
	URL url = null;
	BufferedReader in = null;
	try {
	//url's query inside string 
	url = new URL(urlString);

	//Enter to BufferedReader the whole xml
	in = new BufferedReader(new InputStreamReader(url.openStream()));
	
	//BufferedReader -> String
	String strXml = in.readLine();
	String tmpLine;
	//get xml content data inside strXml
	while ((tmpLine = in.readLine()) != null) 
		strXml = strXml + "\n" + tmpLine;
	
	//parse the xml file gotten using DOM methods
	DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	InputSource is = new InputSource();
	is.setCharacterStream(new StringReader(strXml));
	
    //doc holds the data
    doc = db.parse(is);

	} catch (Exception e) {e.printStackTrace();}
	/*	finally {
	         try {
	        	 in.close();
	         } catch (IOException e) {e.printStackTrace();}

	      } // end 'finally'
	*/}//end of c'tor
	
		
	public static Node getNodeByName(Node fatherNode, String nodeName){
		
		Node node = null;
		
		//get children of root
		NodeList children = fatherNode.getChildNodes();
		
		//if not null
		if(children != null) {
			
			//run over the list
			for(int i = 0; i < children.getLength(); i++ ) {

				if(children.item(i).getNodeName().equals(nodeName)){
					node = children.item(i);
					break;	
				}
			}
		}
		
		return node;
	}
	
	public static String getCharacterDataFromElement(Element e) {
	    Node child = e.getFirstChild();
	    if (child instanceof CharacterData) {
	       CharacterData cd = (CharacterData) child;
	       String str = UrlXMLClass.stringProcess(cd.getData());
	       
	       return str;
	    }
	    return "?";
	  }
	
	protected static String stringProcess(String x)
	{
		x = x.replaceAll("\\s", " ");
		x = x.trim();
		
		int j = 0;
		for(int i = 0; i < x.length(); ++i){
			if(i+1 != x.length()){
				if((x.charAt(i) == ' ') && (x.charAt(i+1) == ' '))
				{}
				else
					++j;
			}
			if(i == x.length()-1)
				if(x.charAt(i) != ' ')
					++j;	

		}
		
		char[] tmp = new char[j];
		
		j = 0;
		for(int i = 0; i < x.length(); ++i){
			if(i+1 != x.length()) {
				if((x.charAt(i) == ' ') && (x.charAt(i+1) == ' '))
				{}
				else {
					tmp[j] = x.charAt(i);
					++j;
				}
			}
			if(i == x.length()-1){
				if(x.charAt(i) != ' ') {
					tmp[j] = x.charAt(i);
					++j;
				}
			}
					
		}
		
		String newS = new String(tmp);
		newS.trim();
		
		return newS;
	}

	public Document getDoc()
	{
		return doc;
	}

}
